package org.ezplatform.goa.goods.web.rest;

import org.ezplatform.cloud.common.condition.AppAuthCondition;
import org.ezplatform.core.annotation.AppAuth;
import org.ezplatform.core.pagination.ExtPageRequest;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.goa.goods.entity.GoaGoodsReceive;
import org.ezplatform.goa.goods.service.GoaGoodsReceiveService;
import org.ezplatform.util.SqlUtils;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ezplatform.core.web.view.OperationResult;
import java.util.ArrayList;

@Controller
@RequestMapping(value = "/api/w/goa/goods/goaGoodsReceive")
@Conditional(AppAuthCondition.class)
@AppAuth(identifier = "goa_goods")
public class GoaGoodsReceiveWebApiController extends BaseController<GoaGoodsReceive, String> {
    @Autowired
    private GoaGoodsReceiveService goaGoodsReceiveService;

    @Override
    protected BaseService<GoaGoodsReceive, String> getEntityService() {
        return goaGoodsReceiveService;
    }

    /**
     * 初始化数据
     *
     * @param request
     * @param model
     * @param id 数据id
     */
    @ModelAttribute
    public void prepareModel(HttpServletRequest request, Model model,
                             @RequestParam(value = "id", required = false) String id) {
        super.initPrepareModel(request, model, id);
    }

    /**
     * 物品领用列表数据显示
     * @param request
     * @return
     */
    @RequestMapping(value = "/goodsReceiveList", method = RequestMethod.GET)
    @ResponseBody
    public Page<Map> getGoodsReceiveList(HttpServletRequest request) {
        Map<String, String> searchParam = new HashMap<>();

        //物品名称
        String goodsName = request.getParameter("goodsName");
        if (StringUtils.isNotEmpty(goodsName)) {
            goodsName = SqlUtils.escapeNormalChars(goodsName);
            searchParam.put("goodsName", goodsName);
        }
        //物品分类
        String classifyName = request.getParameter("classifyName");
        if (StringUtils.isNotEmpty(classifyName)) {
            classifyName = SqlUtils.escapeNormalChars(classifyName);
            searchParam.put("classifyName", classifyName);
        }

        int offset = Integer.parseInt(ValidateUtils.filter(request.getParameter("offset")));
        int page = Integer.parseInt(ValidateUtils.filter(request.getParameter("page")));
        int size = Integer.parseInt(ValidateUtils.filter(request.getParameter("size")));
        ExtPageRequest pageable = new ExtPageRequest(offset, page, size, null);

        List<PropertyFilter> propertyFilters = PropertyFilter.buildFiltersFromHttpRequest(request);
       return this.goaGoodsReceiveService.getGoodsReceiveList(propertyFilters, pageable, searchParam);

    }

    @RequestMapping(value = "/initGoosType", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult initGoosType(HttpServletRequest request) {
        Map<String, String> searchParam = new HashMap<>();
        List<Map> list = this.goaGoodsReceiveService.initGoosType(searchParam);
        return OperationResult.buildSuccessResult("查询成功",list);
    }


    @RequestMapping(value = "/goodsUserList", method = RequestMethod.GET)
    @ResponseBody
    public Page goodsUserList(HttpServletRequest request) {
        String goodsid = request.getParameter("goodsid");
        Map<String,String> map = new HashMap<>();
        map.put("goodsid",goodsid);
        return goaGoodsReceiveService.goodsUserList(map,request);
    }


    @RequestMapping(value = "/getUserFGCS", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult getUserFGCS(HttpServletRequest request) {
        List<Map> list = goaGoodsReceiveService.getUserFGCS();
        return OperationResult.buildSuccessResult("成功",list);
    }


    @RequestMapping(value = "/getLqGoodsByUser", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult getLqGoodsByUser(HttpServletRequest request) {
        //个人领取
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        Map<String,String> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<Map> list = goaGoodsReceiveService.getLqGoodsByUser(map);
        return OperationResult.buildSuccessResult("成功",list);
    }

    @RequestMapping(value = "/getLqGoodsByFGLD", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult getLqGoodsByFGLD(HttpServletRequest request) {
        //分管领导
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        String deputy_leaders = request.getParameter("deputy_leaders");
        String[] leadrsplit = deputy_leaders.split(",");
        //把所有部门下面的人查出来
        List<Map> userList = goaGoodsReceiveService.getOrgInUser(leadrsplit);
        List<String> useridList = new ArrayList<>();
        for(int i =0;i<userList.size();i++){
            useridList.add(userList.get(i).get("id").toString());
        }
        Map<String,String> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);
        List<Map> list = goaGoodsReceiveService.getLqGoodsByUserList(map,useridList);
        return OperationResult.buildSuccessResult("成功",list);
    }
}
